/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.baseDataView;


import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.bean.TShop;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJPasswordFieldComponent;
import com.ruipengkj.component.JLabelJTextAreaComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.component.page.PageDelegale;
import com.ruipengkj.service.TShopService;
import com.ruipengkj.view.InitMain;
import com.ruipengkj.view.base.BasePagePanel;
/**
 * 说明：
 * 商店管理
 * @Author：李智深
 * @Date：2012-03-20
 */
public class DataShopPanel extends BasePagePanel<TShop> implements PageDelegale ,Runnable{
	private static final long serialVersionUID = 1L;
	
	private JLabelJTextFieldComponent ShopID;//商店编号CHAR(15)  PK
	private JLabelJTextFieldComponent Name;//商店名称VARCHAR(200)
	private JLabelJTextFieldComponent TotalNum;//总销售数量INTEGER
	private JLabelJTextFieldComponent TotalMoney;//总销售额DECIMAL(20,3)
	private JLabelJTextAreaComponent Des;//描述VARCHAR(10240)
	private JLabelJTextFieldComponent AD;//广告标语VARCHAR(256)
	private JLabelJTextFieldComponent SmallPic;//标题图片
	private JLabelJTextFieldComponent CatalogID;//所属目录CHAR(15)
	private JLabelJTextFieldComponent Scope;//主营范围VARCHAR(128)
	private JLabelJTextFieldComponent Viewer;//商店展示视图VARCHAR(64)
	private JLabelJTextFieldComponent ViewParam;//展示参数VARCHAR(256)
	private JLabelJTextFieldComponent IsOnlineSale;//是否支付在线交易TINYINT
	private JLabelJTextFieldComponent Phone;//商店电话VARCHAR(32)
	private JLabelJTextFieldComponent OwnerName;//店主姓名CHAR(10)
	private JLabelJTextFieldComponent IsIntimeSale;//是否支持实时外送TINYINT
	private JLabelJTextFieldComponent IsLogisticSale;//是否支持物流
	private JLabelJTextFieldComponent IntimeDistance;//实时外送距离Integer
	private JLabelJTextFieldComponent Runtime;//营业时间VARCHAR(128)
	private JLabelJTextFieldComponent IntimeAmount;//起送金额DECIMAL(20,3)
	private JLabelJTextFieldComponent IntimeCount;//起送份数INTEGER
	private JLabelJTextFieldComponent IsSMSOrder;//订单是否通过短信发送TINYINT
	private JLabelJTextAreaComponent Notice;//公告VARCHAR(1024)
	private JLabelJTextFieldComponent IsAutoUptMemberLevel;//是否自动升级会员级别 boolean
	private JLabelJTextFieldComponent SaleType;//商品类型TINYINT1为可以进货也可以出售的商品  2为进货但不出售的商品 3为只出售不出货的商品 默认为1
	private JLabelJTextFieldComponent Money;//商店资金DECIMAL(20,3)资金的逻辑是系统建立期初有一部分资金（也可为0），采购和销售会引起资金的变化
	private JLabelJTextAreaComponent Address;//商店地址VARCHAR(128)
	private JLabelJTextFieldComponent ZipCode;//邮编CHAR(6)
	private JLabelJTextFieldComponent URL;//网址VARCHAR(256)
	private JLabelJTextFieldComponent EMail;//网址VARCHAR(64)
	private JLabelJTextFieldComponent SiteUserName;//商店用户名
	private JLabelJPasswordFieldComponent SitePwd;//商店密码
	
	private TShopService tShopService;
	private TShop tShop;
	private JButton okButton,celButton;
	private JPanel main_jp;
	public DataShopPanel() {
		tShopService=BuildSpringApplication.getInstance().getTShopServiceImpl();
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.init();
		this.initListener();
	}
	
	private void init() {
		
		main_jp=uiFactory.createBoxJPanel(BoxLayout.Y_AXIS);
		main_jp.setBorder(BorderFactory.createTitledBorder("商店设置"));
		
		ShopID=new JLabelJTextFieldComponent("商  店  编  号：",30);//商店编号CHAR(15)  PK
		ShopID.getText().setText(UUID.GenUUID15());
		ShopID.getText().setEditable(false);
		Name=new JLabelJTextFieldComponent("商  店  名  称：",30);//商店名称VARCHAR(200)
//		TotalNum=new JLabelJTextFieldComponent("",30);//总销售数量INTEGER
//		TotalMoney=new JLabelJTextFieldComponent("",30);//总销售额DECIMAL(20,3)
//		AD=new JLabelJTextFieldComponent("广告标语：",30);//广告标语VARCHAR(256)
//		SmallPic=new JLabelJTextFieldComponent("",30);//标题图片
//		CatalogID=new JLabelJTextFieldComponent("",30);//所属目录CHAR(15)
//		Scope=new JLabelJTextFieldComponent("",30);//主营范围VARCHAR(128)
//		Viewer=new JLabelJTextFieldComponent("",30);//商店展示视图VARCHAR(64)
//		ViewParam=new JLabelJTextFieldComponent("",30);//展示参数VARCHAR(256)
//		IsOnlineSale=new JLabelJTextFieldComponent("",30);//是否支付在线交易TINYINT
		Phone=new JLabelJTextFieldComponent("商  店  电  话：",30);//商店电话VARCHAR(32)
//		OwnerName=new JLabelJTextFieldComponent("",30);//店主姓名CHAR(10)
//		IsIntimeSale=new JLabelJTextFieldComponent("",30);//是否支持实时外送TINYINT
//		IsLogisticSale=new JLabelJTextFieldComponent("",30);//是否支持物流
//		IntimeDistance=new JLabelJTextFieldComponent("",30);//实时外送距离Integer
//		Runtime=new JLabelJTextFieldComponent("",30);//营业时间VARCHAR(128)
//		IntimeAmount=new JLabelJTextFieldComponent("",30);//起送金额DECIMAL(20,3)
//		IntimeCount=new JLabelJTextFieldComponent("",30);//起送份数INTEGER
//		IsSMSOrder=new JLabelJTextFieldComponent("",30);//订单是否通过短信发送TINYINT
		Notice=new JLabelJTextAreaComponent("公          告：",23,2);//公告VARCHAR(1024)
//		IsAutoUptMemberLevel=new JLabelJTextFieldComponent("",30);//是否自动升级会员级别 boolean
//		SaleType=new JLabelJTextFieldComponent("",30);//商品类型TINYINT1为可以进货也可以出售的商品  2为进货但不出售的商品 3为只出售不出货的商品 默认为1
//		Money=new JLabelJTextFieldComponent("",30);//商店资金DECIMAL(20,3)资金的逻辑是系统建立期初有一部分资金（也可为0），采购和销售会引起资金的变化
		Address=new JLabelJTextAreaComponent("商  店  地  址：",23,2);//商店地址VARCHAR(128)
//		ZipCode=new JLabelJTextFieldComponent("",30);//邮编CHAR(6)
		URL=new JLabelJTextFieldComponent("网          址：",30);//网址VARCHAR(256)
//		EMail=new JLabelJTextFieldComponent("",30);//邮箱VARCHAR(64)
		Des=new JLabelJTextAreaComponent("描          述：",23,2);//描述VARCHAR(10240)
		SiteUserName = new JLabelJTextFieldComponent("店联同步用户名：",30);
		SitePwd = new JLabelJPasswordFieldComponent("  店联同步密码：",30);
		main_jp.add(ShopID);
		main_jp.add(Name);
		main_jp.add(Phone);
		main_jp.add(SiteUserName);
		main_jp.add(SitePwd);
		main_jp.add(URL);
		main_jp.add(Notice);
		main_jp.add(Address);
		main_jp.add(Des);
		
		JPanel jp=uiFactory.createFlowJPanel();
		okButton=new RPJButtonBlue("保存(Ctrl+S)");
		celButton=new RPJButtonGray("清空");
		jp.add(okButton);
		jp.add(celButton);
		main_jp.add(jp);
		this.add(main_jp);
	}
	
	//初始化监听器
	private void initListener() {
		okButton.addActionListener(this);
		okButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		celButton.addActionListener(this);
	}
	
	@Override
	public void run() {
		try {
			List<TShop> list=tShopService.findService(null);
			if(list!=null&&list.size()!=0){
				tShop=list.get(0);
				ShopID.getText().setText(tShop.getShopID());
				Name.getText().setText(tShop.getName());
				Phone.getText().setText(tShop.getPhone());
				SiteUserName.getText().setText(tShop.getSiteUserName());
				SitePwd.getText().setText(tShop.getSitePwd());
				URL.getText().setText(tShop.getURL());
				Notice.getArea().setText(tShop.getNotice());
				Address.getArea().setText(tShop.getAddress());
				Des.getArea().setText(tShop.getDes());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==okButton){
			//确定
			String nameStr=Name.getText().getText().trim();
			String phoneStr=Phone.getText().getText().trim();
			String SiteUserNameStr=SiteUserName.getText().getText().trim();
			String SitePwdStr=SitePwd.getText().getText().trim();
			String URLStr=URL.getText().getText().trim();
			String NoticeStr=Notice.getArea().getText().trim();
			String AddressStr=Address.getArea().getText().trim();
			String DesStr=Des.getArea().getText().trim();
			if(nameStr.equals("")||nameStr.length()>200){
				Util.showMessageDialog("商店名称不能为空，且不能大于200个字");
				Name.getText().requestFocus();
				return;
			}
			if(phoneStr.equals("")||phoneStr.length()>32){
				Util.showMessageDialog("商店电话不能为空，且不能大于32个字");
				Phone.getText().requestFocus();
				return;
			}
			if(SiteUserNameStr.equals("")){
				Util.showMessageDialog("用户名不能为空");
				SiteUserName.getText().requestFocus();
				return;
			}
			if(SitePwdStr.equals("")){
				Util.showMessageDialog( "密码不能为空");
				SitePwd.getText().requestFocus();
				return;
			}
			if(URLStr.length()>256){
				Util.showMessageDialog("URL不能大于256个字");
				URL.getText().requestFocus();
				return;
			}
			if(NoticeStr.length()>1024){
				Util.showMessageDialog("公告不能大于1024个字");
				Notice.getArea().requestFocus();
				return;
			}
			if(AddressStr.length()>128){
				Util.showMessageDialog("地址不能大于128个字");
				Address.getArea().requestFocus();
				return;
			}
			if(DesStr.length()>10240){
				Util.showMessageDialog("描述不能大于10240个字");
				Des.getArea().requestFocus();
				return;
			}
			 if(tShop==null){
				 tShop=new TShop();
				 tShop.setShopID(ShopID.getText().getText().trim());
			 }
			 tShop.setName(nameStr);
			 tShop.setPhone(phoneStr);
			 tShop.setSiteUserName(SiteUserNameStr);
			 tShop.setSitePwd(SitePwdStr);
			 tShop.setURL(URLStr);
			 tShop.setNotice(NoticeStr);
			 tShop.setAddress(AddressStr);
			 tShop.setDes(DesStr);
			try {
				tShopService.saveOrUpdateService(tShop);
//				JOptionPane.showMessageDialog(null, "操作成功，如果修改了商店信息，请重新启动软件");
				if (Util.showConfirmDialog("操作成功，需要重启软件生效，现在重启吗？") == 0) {
					Util.getTopWindow(this).dispose();
					thread.start();
				}
			} catch (Exception e1) {
				e1.printStackTrace();
				Util.showMessageDialog("操作失败");
			}
		}else if(e.getSource()==celButton){
			//取消
			Name.getText().setText("");
			Phone.getText().setText("");
			SiteUserName.getText().setText("");
			SitePwd.getText().setText("");
			URL.getText().setText("");
			Notice.getArea().setText("");
			Address.getArea().setText("");
			Des.getArea().setText("");
		}
	}
	
	Thread thread = new Thread() {
		public void run() {
			try {
				Thread.sleep(3000);
				new InitMain().setVisible(true);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		};
	};
	
	public void setShopInfo(){
		String shopName = SiteUserName.getText().getText();
		String shopPwd = SitePwd.getText().getText();
		
	}
	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}
}